/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.sales.generics.client.components.timepicker;

import com.google.gwt.uibinder.client.UiConstructor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.TextBox;
import com.google.inject.Inject;

/**
 *
 * @author v
 */
public class HDateTimePicker extends Composite {

    private final TextBox timePicker = new TextBox();
    private static final String TIMEPICKER_ID_NAME = "datetimepicker";
    private boolean isTime = false;

    @Inject
    public HDateTimePicker() {
        timePicker.getElement().addClassName(TIMEPICKER_ID_NAME);
        initWidget(timePicker);
    }

    @UiConstructor
    public HDateTimePicker(boolean isTime) {
        this.isTime = isTime;
        timePicker.getElement().addClassName(TIMEPICKER_ID_NAME);
        initWidget(timePicker);
        addHandler();
    }

    private void addHandler() {

    }

    public void setReadOnly(boolean readOnly) {
        timePicker.setReadOnly(readOnly);
    }

    public void setValue(String value) {
        timePicker.setValue(value);
    }

    public String getValueAsString() {
        if ("__/__/____".equals(timePicker.getText())) {
            return "";
        }
        return timePicker.getText();
    }

    public void setFocus(boolean isFocused) {
        timePicker.setFocus(isFocused);
    }

    public void clear() {
        setValue(null);
        timePicker.setText(null);
    }

    @Override
    protected void onAttach() {
        super.onAttach();
        createDateTimePicker(isTime);
    }
    
    public void setEnabled(boolean enabled) {
        timePicker.setEnabled(enabled);
    }
        

    private native void createDateTimePicker(boolean isTime)/*-{       
     $wnd.$(function(){
     $wnd.$('.datetimepicker').datetimepicker({
     format:'d/m/Y',
     timepicker:isTime,
     mask:true,
     formatDate:'d/m/Y',
     closeOnDateSelect:true
     });
     });
     }-*/;
}
